home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0920.ZIP / STKDMP.ARC / STAKDUMP.ASM next >
Assembly Source File  |  1987-11-27  |  9KB  |  200 lines

  1. DSEG    SEGMENT BYTE PUBLIC
  2.  
  3.         ;Pascal variables
  4.         EXTRN   InitSP:WORD
  5.         EXTRN   ErrorAddr:DWORD
  6.         EXTRN   PrefixSeg:WORD
  7.  
  8. DSEG    ENDS
  9.  
  10. CSEG    SEGMENT BYTE PUBLIC
  11.  
  12.         ASSUME  CS:CSEG, DS:DSEG
  13.  
  14.         ;Entry point from Pascal
  15.         PUBLIC  Trace
  16.  
  17. ;Table of encoded instruction lengths and types
  18. InstrTable LABEL BYTE
  19. ;           0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
  20.         DB 09, 09, 09, 09, 02, 03, 01, 01, 09, 09, 09, 09, 02, 03, 01, 01   ;0
  21.         DB 09, 09, 09, 09, 02, 03, 01, 01, 09, 09, 09, 09, 02, 03, 01, 01   ;1
  22.         DB 09, 09, 09, 09, 02, 03, 01, 01, 09, 09, 09, 09, 02, 03, 01, 01   ;2
  23.         DB 09, 09, 09, 09, 02, 03, 01, 01, 09, 09, 09, 09, 02, 03, 01, 01   ;3
  24.         DB 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01   ;4
  25.         DB 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01   ;5
  26.         DB 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32   ;6
  27.         DB 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02   ;7
  28.         DB 10, 11, 10, 10, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09   ;8
  29.         DB 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 05, 01, 01, 01, 01, 01   ;9
  30.         DB 03, 03, 03, 03, 01, 01, 01, 01, 02, 03, 01, 01, 01, 01, 01, 01   ;A
  31.         DB 02, 02, 02, 02, 02, 02, 02, 02, 03, 03, 03, 03, 03, 03, 03, 03   ;B
  32.         DB 32, 32, 03, 01, 09, 09, 10, 11, 32, 32, 03, 01, 01, 02, 01, 01   ;C
  33.         DB 09, 09, 09, 09, 01, 01, 32, 01, 09, 09, 09, 09, 09, 09, 09, 09   ;D
  34.         DB 02, 02, 02, 02, 02, 02, 02, 02, 03, 03, 05, 02, 01, 01, 01, 01   ;E
  35.         DB 01, 32, 01, 01, 01, 01, 18, 19, 01, 01, 01, 01, 01, 01, 09, 09   ;F
  36.  
  37. ;********************************************************************** Rm
  38. ; Add length of secondary fields to instruction length
  39. ; On entry:
  40. ;     AH = mod reg r/m byte to decode
  41. ;     BX = instruction length so far
  42. ; On exit
  43. ;     BX = total instruction length including address
  44. ;     AX destroyed
  45. Rm      PROC    NEAR
  46.         MOV     AL,AH                           ;AL = NextB
  47.         AND     AL,0C0h                         ;AL = mod field
  48.         OR      AL,AL                           ;Direct addressing mode?
  49.         JNZ     Rm4                             ;No, check again
  50.         AND     AH,7                            ;AH = r/m field
  51.         CMP     AH,6                            ;No base register mode?
  52.         JNE     Rm1                             ;No, just one byte of instruction
  53.         JMP SHORT Rm3                           ;Else three bytes of instruction
  54. Rm4:    CMP     AL,40h                          ;One byte displacement mode?
  55.         JE      Rm2                             ;Yes, two bytes of instruction
  56.         CMP     AL,80h                          ;Two byte displacement mode?
  57.         JNE     Rm1                             ;No, just one byte of instruction
  58. Rm3:    INC     BX
  59. Rm2:    INC     BX
  60. Rm1:    INC     BX
  61.         RET
  62. Rm      ENDP
  63.  
  64. ;********************************************************************** Next
  65. ; on entry:
  66. ;     ES:DI points to current instruction
  67. ; on exit:
  68. ;     ES:DI points to next instruction
  69. ;     AX,BX destroyed
  70. ;     CF set if disassembly error
  71. Next    PROC    NEAR
  72.         MOV     AX,ES:[DI]                      ;Get next word of code
  73.         MOV     BX,offset InstrTable            ;Point to instruction table
  74.         DB      2Eh                             ;CS override
  75.         XLAT                                    ;AL = instruction code delta
  76.         MOV     BL,AL                           ;Save delta
  77.         XOR     BH,BH                           ;BH = 0
  78.         AND     BL,7                            ;BX = instruction length
  79.         AND     AL,0F8h                         ;Separate instruction type
  80.         OR      AL,AL                           ;Itype = 0?
  81.         JZ      SetNext                         ;Yes, we're done
  82.         CMP     AL,08                           ;Itype = 8?
  83.         JNZ     Check10                         ;No, check further
  84. GetRm:  CALL    Rm                              ;Get addressing length
  85.         JMP SHORT SetNext
  86. Check10:
  87.         CMP     AL,10h                          ;Itype = 10h?
  88.         JNE     NextError                       ;No, junk in code stream
  89.         MOV     AL,AH                           ;AL = Next byte
  90.         AND     AL,38h                          ;NextB and 38h
  91.         OR      AL,AL                           ;Is it zero?
  92.         JZ      GetRm                           ;Yes, add Rm and we're done
  93.         MOV     BX,1                            ;No, base length is one
  94.         JMP SHORT GetRm                         ;Add Rm and we're done
  95. NextError:
  96.         STC                                     ;Set carry flag to indicate error
  97. SetNext:
  98.         ADD     DI,BX                           ;Point DI to next
  99.         RET
  100. Next    ENDP
  101.  
  102. ;********************************************************************** FarProc
  103. ; on entry:
  104. ;     ES:DI points to current instruction
  105. ; on exit:
  106. ;     ZF set if current procedure is NEAR
  107. ;     CF set if disassembly error
  108. ;     AX,BX destroyed
  109. FarProc PROC    NEAR
  110.         PUSH    DI                              ;Save current IP value
  111.         CLC                                     ;Clear error flag
  112. FPNext: MOV     AL,ES:[DI]                      ;Get next instruction
  113.         CMP     AL,0C2h                         ;See if any RET instruction
  114.         JZ      FPDone
  115.         CMP     AL,0C3h
  116.         JZ      FPDone
  117.         CMP     AL,0CAh
  118.         JZ      FPDone
  119.         CMP     AL,0CBh
  120.         JZ      FPDone
  121.         CALL    Next                            ;Advance to next instruction
  122.         JNC     FPNext                          ;Loop if no disassembly error
  123. FPDone:
  124.         TEST    AL,08h                          ;Is FAR bit set?
  125.         POP     DI                              ;Restore current instruction
  126.         RET
  127. FarProc ENDP
  128.  
  129. ;***************************************************************** WriteAxHex
  130. ;Write AX in hex format to stdout
  131. ;Also called at WriteAlAscii
  132. ;  AX,CX,DX destroyed
  133. WriteAxHex PROC NEAR
  134.         PUSH   AX                               ;Save AX a moment
  135.         MOV    AL,AH                            ;Do top byte first
  136.         CALL   DoNibble
  137.         POP    AX                               ;Now do bottom byte
  138. DoNibble:
  139.         PUSH   AX                               ;Save AL
  140.         MOV    CL,04                            ;Get top nibble
  141.         SHR    AL,CL
  142.         CALL   ConvHex                          ;Convert to hex format
  143.         POP    AX
  144.         AND    AL,0Fh                           ;Get bottom nibble
  145. ConvHex:
  146.         ADD    AL,'0'                           ;Convert to ASCII numeral
  147.         CMP    AL,'9'                           ;Is it decimal?
  148.         JBE    WriteAlAscii                     ;Yes, write it now
  149.         ADD    AL,07                            ;Convert to 'A'..'F'
  150. WriteAlAscii:
  151.         MOV    DL,AL                            ;Write character in AL
  152.         MOV    AH,06                            ;DOS writechar
  153.         INT    21h
  154.         RET
  155. WriteAxHex ENDP
  156.  
  157. ;********************************************************************** Trace
  158. ; Trace stack of return points
  159. ; Writes results to stdout, which should be reset first
  160. Trace   PROC    NEAR
  161.         MOV     CX,PrefixSeg
  162.         ADD     CX,10h                          ;CX = base code segment
  163.         LES     DI,ErrorAddr                    ;ES:DI => relative error address
  164.         MOV     AX,ES
  165.         ADD     AX,CX                           ;Convert to absolute address
  166.         MOV     ES,AX                           ;ES:DI => absolute error address
  167.         MOV     SI,[BP]                         ;SI = saved BP
  168. TraceNext:
  169.         PUSH    CX                              ;Save CX
  170.         MOV     AX,ES                           ;Get current code segment
  171.         SUB     AX,CX                           ;Convert to relative code seg
  172.         CALL    WriteAxHex                      ;Write code seg in hex
  173.         MOV     AL,':'
  174.         CALL    WriteAlAscii                    ;Write ':'
  175.         MOV     AX,DI
  176.         CALL    WriteAxHex                      ;Write instr pointer in hex
  177.         MOV     AL,13
  178.         CALL    WriteAlAscii                    ;Write <CR>
  179.         MOV     AL,10
  180.         CALL    WriteAlAscii                    ;Write <LF>
  181.         POP     CX                              ;Restore CX
  182.  
  183.         CMP     SI,InitSP                       ;Stack trace done?
  184.         JAE     TraceDone                       ;Yes, exit
  185.         CALL    FarProc                         ;Is current ES:DI a far proc?
  186.         JC      TraceDone                       ;Get out if disassembly error
  187.         JZ      SetNewIp                        ;Jump if near proc
  188.         MOV     ES,SS:[SI+4]                    ;ES = new code segment
  189. SetNewIp:
  190.         MOV     DI,SS:[SI+2]                    ;DI = new instr pointer
  191.         MOV     SI,SS:[SI]                      ;SI = new BP
  192.         JMP SHORT TraceNext                     ;Do it again
  193. TraceDone:
  194.         RET
  195. Trace   ENDP
  196.  
  197. CSEG    ENDS
  198.  
  199.         END
  200.